#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <curses.h>

#include "consolatoric.h"

int ticker = 0;
char pairs[7][7];

void display_frame() {
    int i,  x, y, bg = 0;
    unsigned char s;
    int lastpair = 1;
    int fg = 7;
    int attr = A_NORMAL;
    ticker++;
    if( ticker >= 252) {
        memset(pairs, 0, 64);
        char* om = (char *) &Oric_Mem[RAM_TEXT];
        init_pair(1, fg, bg);
        pairs[fg][bg] = lastpair;
        for(i = 0; i < 1120 ; i++) {
            x = i%40;
            y = i/40;
            s = om[i];
            //mvprintw(y,41+x*4,"    ");
            //mvprintw(y, 41+x*4, "%0d",s);
            if(s > 127) {  // invers video
                attr = A_REVERSE;
                s -= 128;
            } else  if(s < 32) {  // special attributes
                if(s > 15 && s < 24) bg = s - 16;  // bg color change
                else if(s < 8) fg = s;  // fg color change
                else if(s==12) attr |= A_BLINK;
                if(pairs[fg][bg] == 0) {
                    lastpair +=1;
                    init_pair(lastpair, fg, bg);
                    pairs[fg][bg] = lastpair;
                }
                s = 32;
            }
            if(s == 95) s = 163;
            if(s == 96) s = 169;
            attron(attr|COLOR_PAIR(pairs[fg][bg]));
            mvaddch(y, x, s);
            attroff(attr|COLOR_PAIR(pairs[fg][bg]));
            attr &= !A_REVERSE;  // clear attribute that affects one char
            if(x == 39) {       //clear attributes that affect one line
                bg = 0; fg = 7;
                attr = A_NORMAL;
            }
        }
        ticker = 0;
        refresh();
    }
}

void config_screen() {
    //TODO
}

void printme() {
    puts("we'r in\n");
};
